<?php
$oUsers = UserHandler::getUsers();
$sMissionStatuses = MissionHandler::getStatuses();
?>
<form action="index.php" method="post" name="form_job_search">
<input type="hidden" name="sView" value="job-search">
<table class="boxborder" cellspacing="0">
	<tr>
		<td class="boxhandle" colspan="2"><?php print msg('Search') ?></td>
	</tr>
	<tr>
		<td class="fieldname"><?php print msg('Search string') ?>:</td>
		<td class="fieldvalue"><input type="text" name="sSearch" value="<?php print @$_REQUEST['sSearch'] ?>"> <span class="small">(<?php print msg('supports Regular Expressions') ?>)</span></td>
	</tr>
	<tr>
		<td class="fieldname"><?php print msg('Search in') ?>:</td>
		<td class="fieldvalue nowrap">
			<input type="checkbox" name="bSearchOnlyMine" value="1" <?php print (@$_REQUEST['bSearchOnlyMine'] ? 'checked' : '') ?>><?php print msg('Only what I\'m assigned to') ?><br>
			<input type="checkbox" name="bSearchOnlyVisible" value="1" <?php print (@$_REQUEST['bSearchOnlyVisible'] ? 'checked' : '') ?>><?php print msg('Only what\'s visible (not closed for example)') ?><br>
			<input type="checkbox" name="bSearchInNotes" value="1" <?php print (@$_REQUEST['bSearchInNotes'] ? 'checked' : '') ?>><?php print msg('Notes') ?><br>
		</td>
	</tr>
	<tr>
		<td class="boxbody center" colspan="2">
			<input type="submit" value="<?php print msg('Search') ?>">
		</td>
	</tr>
<?php
if (@$_REQUEST['sSearch'])
{
	$oJobs = JobHandler::getJobs();
	$sStatuses = MissionHandler::getStatuses();
	$iMissionsFound = 0;
	?>
	<tr>
		<td class="clear" colspan="2">
			<table style="width: 100%;" cellspacing="0">
	<?php
	foreach ($oJobs as $oThisJob)
	{
		$bJobShown = false;
		$oMissions = $oThisJob->getMissions(@$_REQUEST['bSearchOnlyVisible'] == false);
		foreach ($oMissions as $oThisMission)
		{
			if (@$_REQUEST['bSearchOnlyMine'] && (!in_array($_SESSION['rid'], $oThisMission->iUsers)))
			{
				continue;
			}
			
			$iNotesFound = array();
			if (@$_REQUEST['bSearchInNotes'])
			{
				$oNotes = $oThisMission->getNotes();
				foreach ($oNotes as $oThisNote)
				{
					if (preg_match('/'.$_REQUEST['sSearch'].'/i', $oThisNote->sBody) > 0)
					{
						$iNotesFound[] = $oThisNote->iID;
					}
				}
			}
			
			if (preg_match('/'.$_REQUEST['sSearch'].'/i', $oThisMission->sTodo) > 0 || count($iNotesFound) > 0)
			{
				$iMissionsFound++;
				$sExtraString = ($sMissionStatuses[$oThisMission->iStatus]['visible'] ? '' : '&fJobVar=showhiddenmissions&fJobValue='.$oThisJob->iID);
				if (!$bJobShown)
				{
					$bJobShown = true;
					?>
					<tr>
						<td class="separator small">
							<a href="index.php?sView=job-view&iJobID=<?php print $oThisJob->iID ?>"><?php print $oThisJob->sName ?></a>
						</td>
						<td class="separator small center"><?php print msg('Status') ?></td>
						<td class="separator small center"><?php print msg('Urgency') ?></td>
						<td class="separator small"><?php print msg('Assignment') ?></td>
					</tr>
					<?php
				}
				?>
				<tr>
					<td class="missionstatus<?php print $oThisMission->iStatus ?> small"><a href="index.php?sView=job-view&iJobID=<?php print $oThisJob->iID ?>&iPopupMission=<?php print $oThisMission->iID.$sExtraString ?>"><?php print htmlspecialchars($oThisMission->sTodo) ?></a></td>
					<td class="missionstatus<?php print $oThisMission->iStatus ?> small center"><?php print $sStatuses[$oThisMission->iStatus]['caption'] ?></td>
					<td class="listentity small center"><?php print $oThisJob->sUrgencies[$oThisMission->iUrgency] ?></td>
					<td class="listentity small">
					<?php
					$bFirst = true;
					foreach ($oThisMission->iUsers as $iThisUser)
					{
						if (!$bFirst)
						{
							echo ', ';
						}
						$bFirst = false;
						echo '<a href="index.php?sView=user-view&iUser='.$iThisUser.'">'.$oUsers[$iThisUser]->sUser.'</a>';
					}
					?>
							&nbsp;
					</td>
				</tr>
				<?php
				foreach ($iNotesFound as $iThisNote)
				{
					?>
					<tr>
						<td class="clear" colspan="4">
							<table cellspacing="0">
								<tr>
									<td class="note small">
					<?php
					print $oUsers[$oNotes[$iThisNote]->iUser]->sUser.' :';
					if ($oNotes[$iThisNote]->bUseHTML) {
						print nl2br(stripslashes($oNotes[$iThisNote]->sBody));
					}
					else {
						print nl2br(htmlspecialchars(stripslashes($oNotes[$iThisNote]->sBody)));
					}
					?>
									</td>
								</tr>
							</table>
						</td>
					</tr>
					<?php
				}
			}
		}
	}
	?>
			</table>
		</td>
	</tr>
	<tr>
		<td class="subentity center" colspan="2">
			<?php print ($iMissionsFound > 0 ? ($iMissionsFound == 1 ? $iMissionsFound.' '.msg('task found') : $iMissionsFound.' '.msg('tasks found')) : msg('Nothing was found')) ?>
		</td>
	</tr>
	<?php
}
?>
</table>
<script language="JavaScript">
	document.form_job_search.sSearch.focus();
</script>
</form>
